package com.edooteach.tencent.web.controller.auth.corp;

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.wechat.entity.token.Token;
import com.edooteach.tencent.accessor.CorpTokenAssessor;
import com.edooteach.tencent.entity.corpuser.CorpUser;
import com.edooteach.tencent.handle.CorpUserHandler;

/**
 * 
 * @author mohanlan
 *
 */
@RestController
@RequestMapping("/corp/users")
public class CorpUserRestController {
    
    private static final Logger logger = LoggerFactory.getLogger(CorpUserRestController.class);
    
    @Autowired
    private CorpTokenAssessor corpTokenAssessor;
    
    @Autowired
    private CorpUserHandler corpUserHandler;
    
    /**
     * @param corpUserRequest
     * @return
     */
    @RequestMapping(value="",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult createUser(@RequestBody CorpUser corpUserRequest) {
        HttpResult httpResult = new HttpResult();
        
        try {
            // 1. validate corpUserRequest
            if(!corpUserRequest.isValid()) {
                throw BusinessException.newInstance("400", "参数异常");
            }
            
            Token token = corpTokenAssessor.requestAccessToken();
            httpResult = corpUserHandler.createUser(token.getAccessToken(), corpUserRequest);
            
        } catch (Exception e) {
            logger.error("do send corp message occurs error: ", e);
            httpResult.fail("500").message("创建成员异常");
        }
        return httpResult;
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping(value="",method=RequestMethod.PATCH,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult updateUser(@RequestBody CorpUser corpUserRequest) {
        HttpResult httpResult = new HttpResult();
        try {
            // 1. validate corpUserRequest
            if(!corpUserRequest.isValid()) {
                throw BusinessException.newInstance("400", "参数异常");
            }
            
            Token token = corpTokenAssessor.requestAccessToken();
            httpResult = corpUserHandler.updateUser(token.getAccessToken(), corpUserRequest);
            
        } catch (Exception e) {
            logger.error("do send corp message occurs error: ", e);
            httpResult.fail("500").message("更新成员异常");
        }
        return httpResult;
    }
    
    /**
     * @param userid
     * @return
     */
    @RequestMapping(value="",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_UTF8_VALUE,params="userid")
    public HttpResult loadUser(@RequestParam("userid") String userid) {
        HttpResult httpResult = new HttpResult();
        
        try {
            
            Token token = corpTokenAssessor.requestAccessToken();
            httpResult = corpUserHandler.loadUser(token.getAccessToken(), userid);
            
        } catch (Exception e) {
            logger.error("do send corp message occurs error: ", e);
            httpResult.fail("500").message("读取成员异常");
        }
        return httpResult;
    }
    
    /**
     * @param userid
     * @return
     */
    @RequestMapping(value="",method=RequestMethod.DELETE,produces=MediaType.APPLICATION_JSON_UTF8_VALUE,params="userid")
    public HttpResult deleteUser(@RequestParam("userid") String userid) {
        HttpResult httpResult = new HttpResult();
        try {
            
            Token token = corpTokenAssessor.requestAccessToken();
            httpResult = corpUserHandler.deleteUser(token.getAccessToken(), userid);
            
        } catch (Exception e) {
            logger.error("do send corp message occurs error: ", e);
            httpResult.fail("500").message("删除成员异常");
        }
        return httpResult;
    }
    
    /**
     * @param userids
     * @return
     */
    @RequestMapping(value="",method=RequestMethod.DELETE,produces=MediaType.APPLICATION_JSON_UTF8_VALUE,params="userids")
    public HttpResult deleteUsers(@RequestParam("userids") List<String> userids) {
        HttpResult httpResult = new HttpResult();

        try {
            
            Token token = corpTokenAssessor.requestAccessToken();
            httpResult = corpUserHandler.deleteUsers(token.getAccessToken(), userids);
            
        } catch (Exception e) {
            logger.error("do send corp message occurs error: ", e);
            httpResult.fail("500").message("批量删除成员异常");
        }
        return httpResult;
    }
    
    /**
     * @param departmentId
     * @return
     */
    @RequestMapping(value="",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_UTF8_VALUE,params="departmentId")
    public HttpResult getDepartmentUsers(@RequestParam("departmentId")Integer departmentId) {
        HttpResult httpResult = new HttpResult();
        try {
          
            Token token = corpTokenAssessor.requestAccessToken();
            httpResult = corpUserHandler.getDepartmentUsers(token.getAccessToken(), departmentId);
            
        } catch (Exception e) {
            logger.error("do send corp message occurs error: ", e);
            httpResult.fail("500").message("获取部门成员异常");
        }
        return httpResult;
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping(value="/details",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_UTF8_VALUE,params="departmentId")
    public HttpResult getDetaildDepartmentUsers(@RequestParam("departmentId")Integer departmentId) {
        HttpResult httpResult = new HttpResult();
        try {
            
            Token token = corpTokenAssessor.requestAccessToken();
            httpResult = corpUserHandler.getDetaildDepartmentUsers(token.getAccessToken(), departmentId);
            
        } catch (Exception e) {
            logger.error("do send corp message occurs error: ", e);
            httpResult.fail("500").message("获取部门成员详情异常");
        }
        return httpResult;
    }
}
